package com.amc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.amc.entity.Merchant;
import com.amc.service.IMerchantService;
import com.amc.service.impl.MerchantServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Servlet implementation class GetStatusServlet
 */
public class GetStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private IMerchantService service = new MerchantServiceImpl();
	private ObjectMapper mapper = new ObjectMapper();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetStatusServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		getStatus(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		getStatus(request, response);
	}

	private void getStatus(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, Object> resultMap = new HashMap<>();
		PrintWriter writer = response.getWriter();
		String merchantId = request.getParameter("merchantId");
		Merchant merchant = service.queryByMerchantId(merchantId);
		//request.setAttribute("merchant", merchant);
		if(merchant != null){
			resultMap.put("isBlack", merchant.getIsBlack()+"");
			resultMap.put("remarks", merchant.getRemarks());
			resultMap.put("auditStatus", merchant.getAuditStatus());
		}
		
		String json = mapper.writeValueAsString(resultMap);
		writer.write(json);
		writer.close();
	}
}
